package com.atguigu.gmall.product.rpc;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.SkuImage;
import com.atguigu.gmall.product.entity.SkuInfo;
import com.atguigu.gmall.product.entity.SpuSaleAttr;
import com.atguigu.gmall.product.service.BaseCategory1Service;
import com.atguigu.gmall.product.service.SkuImageService;
import com.atguigu.gmall.product.service.SkuInfoService;
import com.atguigu.gmall.product.service.SpuSaleAttrService;
import com.atguigu.gmall.weball.vo.CategoryTreeVo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;


@RequestMapping("/api/inner/rpc/product")
@RestController
public class SkuRpcController {
    @Autowired
    SkuInfoService skuInfoService;
    @Autowired
    SkuImageService skuImageService;
    @Autowired
    SpuSaleAttrService spuSaleAttrService;

    /**
     * 获取skuinfo
     *
     * @param skuId
     * @return
     */
    @GetMapping("/skuinfo/{skuId}")
    public Result<SkuInfo> getSkuInfo(@PathVariable("skuId") Long skuId) {
        SkuInfo skuInfo = skuInfoService
                .getById(skuId);
        return Result.ok(skuInfo);
    }

    // 获取sku-images
    @GetMapping("/skuimage/{skuId}")
    public Result<List<SkuImage>> getSkuImages(@PathVariable("skuId") Long skuId) {
        List<SkuImage> skuImages = skuImageService
                .lambdaQuery()
                .eq(SkuImage::getSkuId, skuId)
                .list();
        return Result.ok(skuImages);
    }

    // 得到价格
    @GetMapping("/skuprice/{skuId}")
    public Result<BigDecimal> getPrice(@PathVariable("skuId") Long skuId) {
        BigDecimal price = skuInfoService.lambdaQuery().select(SkuInfo::getPrice).eq(SkuInfo::getId, skuId).one().getPrice();
        return Result.ok(price);
    }

    // 得到销售属性+值
    @GetMapping("/skuSaleAttr/{spuId}/{skuId}")
    public Result<List<SpuSaleAttr>> getSpuSaleAttr(@PathVariable("spuId") Long spuId, @PathVariable("skuId") Long skuId) {
        List<SpuSaleAttr> saleAttrs = spuSaleAttrService.getSpuSaleAttrListOrder(spuId, skuId);
        return Result.ok(saleAttrs);
    }

    @GetMapping("/valuesSkuJson/{spuId}")
    public Result<String> getValuesSkuJson(@PathVariable("spuId") Long spuId) {
        String valuesSkuJson = spuSaleAttrService.getvaluesSkuJson(spuId);
        return Result.ok(valuesSkuJson);
    }
}
